{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# Creating Pandapower Networks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "This Tutorial will introduce the user into the pandapower datastructure and how to create networks through the pandapower API. The following minimal example contains the most commont elements that are supported by the Pandapower format. For an example that contains all pandapower elements (3 winding transformers, ward equivalents, impedances), see the [advanced tutorial](create_advanced.ipynb) for network creation.\n",
    "\n",
    "<img src=\"pics/example_network_simple.png\">\n",
    "\n",
    "The datastructure of the pandapower framework is based on the python library pandas. A pandapower network consist of a separate element table for each element type that is used in the network. Each element table consists of a column for each parameter and a row for each element. By executing the follwing code cells you generate the various element tables.  You can find detailed descriptions about each parameter in the pandapower documentation under bulletpoint \"Datastructures and Elements\"."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Empty Network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, we import pandapower an create an empty network:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandapower.create import (\n",
    "    create_empty_network,\n",
    "    create_bus,\n",
    "    create_ext_grid,\n",
    "    create_transformer,\n",
    "    create_line,\n",
    "    create_switch,\n",
    "    create_load,\n",
    "    create_sgen,\n",
    "    create_gen,\n",
    "    create_shunt\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = create_empty_network() #create an empty network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Buses"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"pics/example_network_simple_buses.png\">\n",
    "\n",
    "We now create the three high voltage (vn_kv=110.) and four medium voltage (vn_kv=20.) buses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:10.108751Z",
     "start_time": "2025-10-17T09:05:10.083915Z"
    },
    "scrolled": true,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "bus1 = create_bus(net, name=\"HV Busbar\", vn_kv=110, type=\"b\")\n",
    "bus2 = create_bus(net, name=\"HV Busbar 2\", vn_kv=110, type=\"b\")\n",
    "bus3 = create_bus(net, name=\"HV Transformer Bus\", vn_kv=110, type=\"n\")\n",
    "bus4 = create_bus(net, name=\"MV Transformer Bus\", vn_kv=20, type=\"n\")\n",
    "bus5 = create_bus(net, name=\"MV Main Bus\", vn_kv=20, type=\"b\")\n",
    "bus6 = create_bus(net, name=\"MV Bus 1\", vn_kv=20, type=\"b\")\n",
    "bus7 = create_bus(net, name=\"MV Bus 2\", vn_kv=20, type=\"b\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bus 3 and bus 4 are classified as nodes (type=\"n\"), all other bus types are declared as busbars (type=\"b\"):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:10.264671Z",
     "start_time": "2025-10-17T09:05:10.238323Z"
    }
   },
   "outputs": [],
   "source": [
    "net.bus # show bus table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All create functions return the pandapower index of the element that was created, for example the variable bus6 is now equal to the index of the bus with the name \"MV Station 2\" (which is 5):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:10.855437Z",
     "start_time": "2025-10-17T09:05:10.845320Z"
    }
   },
   "outputs": [],
   "source": [
    "bus6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use these variables for creating bus and branch elements in the following."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### External Grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"pics/example_network_simple_ext_grid.png\">\n",
    "\n",
    "We now create an external grid connection that serves as slack node for the power flow calculation. The voltage of the external grid is set to a magnitude of 1.02 per unit and 50 degrees voltage angle:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:11.010303Z",
     "start_time": "2025-10-17T09:05:10.988592Z"
    }
   },
   "outputs": [],
   "source": [
    "create_ext_grid(net, bus1, vm_pu=1.02, va_degree=50) # Create an external grid connection\n",
    "\n",
    "net.ext_grid #show external grid table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformer "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"pics/example_network_simple_trafo.png\">\n",
    "\n",
    "The transformer connects the high-voltage with the medium-voltage side of the grid. The high-voltage bus of the transformer is connected to Bus3 and on the medium-voltage side the transformer is linked to Bus4. We select the standard type \"25 MVA 110/20 kV\" from the pandapower basic standard type library:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:11.214472Z",
     "start_time": "2025-10-17T09:05:11.190926Z"
    }
   },
   "outputs": [],
   "source": [
    "trafo1 = create_transformer(net, bus3, bus4, name=\"110kV/20kV transformer\", std_type=\"25 MVA 110/20 kV\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " The detailled transformer parameters, such as short circuit voltages, rated power or iron losses are automatically loaded from the standard type library (see [standard type library tutorial](std_types.ipynb)) and stored in the transformer table:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:11.322285Z",
     "start_time": "2025-10-17T09:05:11.296955Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net.trafo #show transformer table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The network includes three medium voltage lines and one high voltage line. The bus connections and line lengths are defined in the network diagram:\n",
    "\n",
    "<img src=\"pics/example_network_simple_lines.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The line parameters are once again taken from the standard type library (see [standard type library tutorial](std_types.ipynb)). We use different line lengths and standard types for each line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:11.506113Z",
     "start_time": "2025-10-17T09:05:11.467429Z"
    }
   },
   "outputs": [],
   "source": [
    "line1 = create_line(net, bus1, bus2, length_km=10, std_type=\"N2XS(FL)2Y 1x300 RM/35 64/110 kV\",  name=\"Line 1\")\n",
    "line2 = create_line(net, bus5, bus6, length_km=2.0, std_type=\"NA2XS2Y 1x240 RM/25 12/20 kV\", name=\"Line 2\")\n",
    "line3 = create_line(net, bus6, bus7, length_km=3.5, std_type=\"48-AL1/8-ST1A 20.0\", name=\"Line 3\")\n",
    "line4 = create_line(net, bus7, bus5, length_km=2.5, std_type=\"NA2XS2Y 1x240 RM/25 12/20 kV\", name=\"Line 4\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The full line table looks like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:11.630807Z",
     "start_time": "2025-10-17T09:05:11.594423Z"
    }
   },
   "outputs": [],
   "source": [
    "net.line # show line table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Switches"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two circuit breakers on the high- and low voltage side of the transformer which connect two buses (Bus2-Bus3 and Bus4-Bus5). These bus-bus switches can be defined with et=\"b\". \n",
    "<img src=\"pics/example_network_simple_switches.png\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:11.957614Z",
     "start_time": "2025-10-17T09:05:11.931573Z"
    }
   },
   "outputs": [],
   "source": [
    "sw1 = create_switch(net, bus2, bus3, et=\"b\", type=\"CB\", closed=True)\n",
    "sw2 = create_switch(net, bus4, bus5, et=\"b\", type=\"CB\", closed=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Furthermore, we equip all bus/line connections in the medium voltage level with load break switches (\"LBS\") as shown in the network diagram. Bus/Line switches are defined with et=\"l\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:12.082886Z",
     "start_time": "2025-10-17T09:05:12.059178Z"
    }
   },
   "outputs": [],
   "source": [
    "sw3 = create_switch(net, bus5, line2, et=\"l\", type=\"LBS\", closed=True)\n",
    "sw4 = create_switch(net, bus6, line2, et=\"l\", type=\"LBS\", closed=True)\n",
    "sw5 = create_switch(net, bus6, line3, et=\"l\", type=\"LBS\", closed=True)\n",
    "sw6 = create_switch(net, bus7, line3, et=\"l\", type=\"LBS\", closed=False)\n",
    "sw7 = create_switch(net, bus7, line4, et=\"l\", type=\"LBS\", closed=True)\n",
    "sw8 = create_switch(net, bus5, line4, et=\"l\", type=\"LBS\", closed=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The switch table now shows all switches. The bus colum contains the index of the bus the switch is connected to. For bus switches (et=\"b\"), the element column contains the index of the second bus the switch connects to. For line switches (et=\"l\"), the element column contains the index of the line the switch connects to. All switches are closed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:12.161201Z",
     "start_time": "2025-10-17T09:05:12.150895Z"
    }
   },
   "outputs": [],
   "source": [
    "net.switch # show switch table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load\n",
    "\n",
    "<img src=\"pics/example_network_simple_load.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The load element is used to model by default constant active and reactive power consumption. We create a 2 MW / 4 MVar load with a scaling factor of 0.6:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:12.285602Z",
     "start_time": "2025-10-17T09:05:12.260656Z"
    }
   },
   "outputs": [],
   "source": [
    "create_load(net, bus7, p_mw=2, q_mvar=4, scaling=0.6, name=\"load\")\n",
    "\n",
    "net.load"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Voltage dependent loads - ZIP load model\n",
    "One can observe load parameters `const_z_percent` and `const_i_percent` which are set to 0 by default. They can be used in order to define part of a load which is voltage-dependent using the so-called **ZIP load** model, which allows a load definition as a composition of constant power, constant current and constant impedance.  \n",
    "\n",
    "As an example, we create a 2 MW / 4 MVar load with 30% of constant impedance and 20% of constant current:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:12.616671Z",
     "start_time": "2025-10-17T09:05:12.591223Z"
    }
   },
   "outputs": [],
   "source": [
    "create_load(net, bus7, p_mw=2, q_mvar=4, const_z_percent=30, const_i_percent=20, name=\"zip_load\")\n",
    "\n",
    "net.load"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Static Generator\n",
    "\n",
    "<img src=\"pics/example_network_simple_sgen.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The static generator element is used to model constant active and reactive power generation. Since the [signing system](https://pandapower.readthedocs.io/en/latest/_images/p_q_reference_system.png) used in pandapower for generation units is from a generator's point of view, the active power has to be positive to model generation. We create a static generator with 2 MW generation and 500 kVar."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:12.958201Z",
     "start_time": "2025-10-17T09:05:12.907446Z"
    }
   },
   "outputs": [],
   "source": [
    "create_sgen(net, bus7, p_mw=2, q_mvar=-0.5, name=\"static generator\")\n",
    "\n",
    "net.sgen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Voltage controlled Generator\n",
    "\n",
    "<img src=\"pics/example_network_simple_gen.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The generator element is used to model voltage controlled active power generation. We define it with a active power generation (positive) and a voltage set point:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:13.331844Z",
     "start_time": "2025-10-17T09:05:13.309242Z"
    }
   },
   "outputs": [],
   "source": [
    "create_gen(net, bus6, p_mw=6, max_q_mvar=3, min_q_mvar=-3, vm_pu=1.03, name=\"generator\") \n",
    "\n",
    "net.gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Shunt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "\n",
    "<img src=\"pics/example_network_simple_shunt.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The shunt is defined by its active and reactive power consumption at rated voltage. Once again, the signing system is from a consumers point of view. We want to model a a capacitator bank, and therefore have to assign a negative reactive power to the shunt:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:05:13.690093Z",
     "start_time": "2025-10-17T09:05:13.659393Z"
    }
   },
   "outputs": [],
   "source": [
    "create_shunt(net, bus3, q_mvar=-0.96, p_mw=0, name='Shunt')\n",
    "\n",
    "net.shunt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to learn how to create more complicated networks, continue with the [advanced create tutorial](create_advanced.ipynb). If you want to learn about how to run a loadflow, continue with the [power flow tutorial](powerflow.ipynb). "
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
